﻿Option Strict On
Option Explicit On

Partial Class private_secure_members_pages_default2
    Inherits System.Web.UI.Page

    Dim u As MembershipUser
    Dim personId As Int32
    Dim entityid As Integer

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try

            u = Membership.GetUser(HttpContext.Current.User.Identity.Name)
            If u Is Nothing Then
                'Force the user to Log in again
                FormsAuthentication.SignOut()
                Response.Redirect("~/Account/login.aspx")
            End If



            If Not Page.IsPostBack Then
                If Not Session("EntityID") Is Nothing Then
                    ddlEntity.SelectedValue = CStr(Session("EntityID"))
                End If
            End If

            Dim getpersonid As New GetPersonIdFromUserId()
            Try

                personId = getpersonid.GetPersonId(CType(u.ProviderUserKey, Guid))
                Dim entityidTA As New LogginsLogDSTableAdapters.usp_GetEntityListFull_ByPersonIdTableAdapter()
                'Dim entityidDT As LogginsLogDS.usp_GetEntityListFull_ByPersonIdDataTable
                'entityidDT = entityidTA.GetData(personId)
                'Me.ddlEntity.DataBind()

            Catch ex As Exception
                Throw ex
            End Try

            Dim strDisplayName As String = ""
            Dim strUserPlan As String = ""

            Me.lblUserDisplay.Text = strDisplayName + " " + strUserPlan
        Catch ex As Exception
            Throw ex
        End Try
        BindLabels()
    End Sub
    Protected Sub SqlEntity_ds_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlEntity_ds.Selecting
        e.Command.Parameters("@PersonId").Value = CType(personId, Integer)
    End Sub

    Protected Sub ddlEntity_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlEntity.SelectedIndexChanged
        If CInt(ddlEntity.SelectedValue) = 0 Then
            Session.Remove("EntityID")
        Else
            Session("EntityID") = ddlEntity.SelectedValue
        End If
    End Sub
    Sub BindLabels()
        Try

            Dim spTA As New LogginsLogDSTableAdapters.usp_dashboard_overviewTableAdapter()
            Dim spDT As New LogginsLogDS.usp_dashboard_overviewDataTable

            If CDbl(ddlEntity.SelectedValue) > 0 OrElse CStr(Session("EntityID")) <> "0" AndAlso CStr(Session("EntityID")) <> "" Then

                Dim persistantentityid As String
                If ddlEntity.SelectedValue = "0" Then
                    persistantentityid = CStr(Session("EntityID"))
                Else
                    persistantentityid = ddlEntity.SelectedValue
                End If


                spDT = spTA.GetData(CType(personId, Integer), CType(persistantentityid, Integer?))

                For Each spDTRow As LogginsLogDS.usp_dashboard_overviewRow In spDT

                    'Entity
                    Me.lblEntityCount.Text = CStr(spDTRow.EntityCount)

                    ' Associates
                    Me.lblAssociateRequests.Text = CStr(spDTRow.AssociateRequestsMade)
                    Me.lblAssociatesCount.Text = CStr(spDTRow.AssociateCount)
                    Me.lblAssociatesNotUsed.Text = CStr(spDTRow.AssociatesNotYetLoggedIn)
                    Me.lblAssociatesActive.Text = CStr(spDTRow.AssociatesActive)

                    'Campaign
                    Me.lblCampaignCount.Text = CStr(spDTRow.CampaignCount)
                    Me.lblCampaignFilterCount.Text = CStr(spDTRow.CampaignFilterCount)

                    'Credits
                    Me.lblCreditsRemaining.Text = CStr(spDTRow.CreditsRemaining)
                    Me.lblCreditsPurchased.Text = CStr(spDTRow.CreditsUsed)
                    Me.lblCreditsUsed.Text = CStr(spDTRow.CreditsPurchased)

                    'Lists & Recipients
                    Me.lblListsCount.Text = CStr(spDTRow.PersonListCount)
                    Me.lblRecipientsCount.Text = CStr(spDTRow.RecipientsCount)

                    'Single Use Tokens
                    Me.lblSingleUseCount.Text = CStr(spDTRow.SingleTokenLoginsCount)
                    Me.lblSingleUseUsed.Text = CStr(spDTRow.SingleTokenLoginsUsed)

                Next
            End If

        Catch ex As Exception
            Throw ex
        End Try

    End Sub


End Class
